package labb1.fordjupn;

import labb1.del2.Card;
import labb1.del2.Hand;

import java.util.ArrayList;
import java.util.Random;

/**
 * @author Mikko Puustinen (mikko.puustinen@gmail.com)
 * @version 0.1, 2007-nov-01
 */
public abstract class Player extends Hand implements Comparable<Player> {
    private String name;
    private int score = 0;
    private int wins = 0;

    /**
     * Default constructor. Creates a random name to the Player. Use the
     * setName(String) method to change the name.
     */
    public Player() {
        super();
        this.name = computeName();
    }

    /**
     * Create a Player with the chosen name.
     *
     * @param name the name.
     */
    public Player(String name) {
        super();
        this.name = name;
    }

    /**
     * Creates a random name if the user used the default constructor.
     *
     * @return the randomly created name.
     */
    private String computeName() {
        String c = "bcdfghjklmnprstvx";
        String v = "aoueiy";
        String n = "";
        Random ran = new Random();
        for (int i = 0, j = -1; i < 3; i++) {
            while ((j = ran.nextInt()) < 0) {
            }

            n += c.charAt(j % c.length());
            j = -1;
            while (j < 0) {
                j = ran.nextInt();
            }

            n += v.charAt(j % v.length());
        }
        n += ran.nextInt(99);
        n = n.substring(0, 1).toUpperCase() + n.substring(1);
        return n;
    }

    /**
     * Returns a clone of all the Cards in the hand.
     *
     * @return
     */
    public ArrayList<Card> getAllCards() {
        ArrayList<Card> clone = new ArrayList<Card>();

        for (int i = 0; i < getNoOfCard(); i++) {
            clone.add(showCard(i));
        }

        return clone;
    }

    /**
     * Returns the number of Cards with the same value.
     *
     * @param cardValue the value to search for and return number of Cards.
     *
     * @return the number of Cards found.
     */
    public int numberOfSameValues(int cardValue) {
        int numOfValues = 0;

        for (Card card : getAllCards()) {
            if (card.getValue() == cardValue)
                numOfValues++;
        }

        return numOfValues;
    }

    /**
     * Set the name of the Player.
     *
     * @param name the name of the Player.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the name of the Player.
     *
     * @return the name.
     */
    public String getName() {
        return name;
    }

    /**
     * Adds the specified score to the Player's score.
     *
     * @param score the score to be added.
     */
    public void addScore(int score) {
        this.score += score;
    }

    /**
     * Get the actual game score.
     *
     * @return the score the Player has in the ongoing game.
     */
    public int getScore() {
        return score;
    }

    /**
     * Adds 1 to the won games of the Player.
     */
    public void addWonGame() {
        wins++;
    }

    /**
     * Get how many times the Player has won the game.
     *
     * @return number of won games.
     */
    public int getWins() {
        return wins;
    }

    /**
     * Empty the hand and reset the score.
     */
    public void resetPlayer() {
        emptyHand();
        score = 0;
    }

    public int compareTo(Player p) {
        if (this.wins < p.getWins())
            return -1;
        else if (this.wins > p.getWins())
            return 1;
        else
            return 0;
    }
}
